package cn.stronglink.assetmanage.business.check.model;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.dao.AssetDao;
import cn.stronglink.assetmanage.dao.CheckDao;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.netrequest.business.check.CheckService;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static android.webkit.WebViewDatabase.getInstance;

/**
 * 待同步数据操作（只同步已经入库的数据，同步后的数据需要在本地删除）
 * Created by chencongcong on 2018/6/14.
 */

public class SyncModel {
    // 获取所有入库待同步的数据 status对应的状态还不确定，暂时定4为入库
    public List<Check> getDataByOrderNoDistince() {
        List<Check> checkList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getCheckDao().queryBuilder().distinct()
                .orderDesc(CheckDao.Properties.CreateTime).
                        where(CheckDao.Properties.Status.eq(Constants.STATUS_FOUR)).list();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (checkList != null) {
            return checkList;
        }
        return null;
    }

    public List<Asset> getAssetByOrderNoDistince(String orderNo){
        return DaoManager.getDaoSession(MyApplication.APP_CONTEXT).
                getAssetDao().queryBuilder().where (AssetDao.Properties.OrderNo.eq(orderNo)).list();
    }

    /**
     * 删除已经同步的资产数据
     */
    public void deleteSyncData(List<Check> checkList) {
        if (checkList != null && checkList.size() > 0) {
            for (int i = 0; i < checkList.size(); i++) {
                Check check = checkList.get(i);
                List<Asset> assetList= DaoManager.getDaoSession(MyApplication.APP_CONTEXT).
                        getAssetDao().queryBuilder().
                        where(AssetDao.Properties.OrderNo.eq(check.getId_())).list();

                DaoManager.getDaoSession(MyApplication.APP_CONTEXT).
                        getAssetDao().deleteInTx(assetList);
            }
            DaoManager.
                    getDaoSession(MyApplication.APP_CONTEXT).getCheckDao().deleteInTx(checkList);
        }

        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
    }
}
